package com.ruoyi.cms.merit.service.impl;

import com.ruoyi.cms.merit.domain.Merit;
import com.ruoyi.cms.merit.mapper.MeritMapper;
import com.ruoyi.cms.merit.service.MeritService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MeritServiceImpl implements MeritService {

    @Autowired
    private MeritMapper meritMapper;

    private final Integer MERIT_ID = 1;

    @Override
    @Transactional(readOnly = true)
    public Long getMeritCount() {
        Merit merit = meritMapper.selectMeritById(Long.valueOf(MERIT_ID));
        if (merit == null) {
            return 0L;
        }
        return merit.getCount();
    }

    @Override
    @Transactional
    public Long incrementMerit() {
        Merit merit = meritMapper.selectMeritById(Long.valueOf(MERIT_ID));
        if (merit == null) {
            merit = new Merit();
            merit.setCount(1L);
            meritMapper.insertMerit(merit);
            // 插入后id会自动回填，这里无须设置id
        } else {
            merit.setCount(merit.getCount() + 1);
            meritMapper.updateMerit(merit);
        }
        return merit.getCount();
    }
}
